<?php

namespace App\Model;


class RefundHistory extends Model
{
//    use TimeZoneTrait;

    public bool $timestamps = true;

    protected ?string $table = 'refund_history';

    protected array $fillable = [
        'id','order', 'fee', 'status','result','refund_order_no','result'
    ];

    protected array $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
    ];

    public function refundBalanceSuccess($data)
    {
        if ($data['status'] == 'succeeded') {
            $this->status = 1;
            $this->result = json_encode($data);
            $order = BalanceOrder::where('number', $this->order)->first();
            $order->return_fee += $data['refund_amt'];
            $order->status = BalanceOrder::STATUS_REFUNDED;
            $order->save();
            $user = User::where('id', $order->user_id)->first();
            $user->balance -= $data['refund_amt'];
            $user->save();
        } else {
            $this->status = 2;
        }
        $this->save();
    }

}
